﻿package
{
	import b3.*;
	import b3.model.*;
	import b3.net.*;
	import b3.scene.*;
	import b3.scene.home.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.guide.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	
	[SWF(width='760', height='600', backgroundColor='#CECAB1', frameRate='30')]
	public class B3 extends MovieClip
	{
		public function B3() 
		{
			//调试面板
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			var param:Object = loaderInfo.parameters;
			//人人网用户
			if (param.hasOwnProperty("xn_sig_session_key"))
			{
				//人人用户信息
				Profile.xn_sig_session_key = param["xn_sig_session_key"];
				//调试信息
				DebugPane.getInstance().addInfo("xn_sig_session_key:" + param["xn_sig_session_key"]);
				//开始进入
				startAll();
			}
			//尚未授权
			else
			{
				//本地调试用
				if (Profile.DEBUG == 1)
				{
					Profile.xn_sig_session_key = "1";
					startAll();
				}
				//发布版本
				else
				{
//					var authenMessage:AuthenMessage = new AuthenMessage();
//					authenMessage.x = (BFont.stageWidth - authenMessage.width) / 2;
//					authenMessage.y = (BFont.stageHeight - authenMessage.height) / 3;
//					addChild(authenMessage);
				}
			}
			stage.addEventListener(Event.FULLSCREEN, onFullScreen);
		}
		
		public function onKeyDown(e:KeyboardEvent):void 
		{
			if (e.keyCode == 27)
			{
				if (DebugPane.getInstance().parent == null)
				{
					addChild(DebugPane.getInstance());
				}
				else
				{
					DebugPane.getInstance().close();
				}
			}
		}
		
		private function onFullScreen(event:Event):void
		{
			if (stage.displayState == StageDisplayState.FULL_SCREEN)
			{
				BFont.stageWidth = stage.fullScreenWidth;
				BFont.stageHeight = stage.fullScreenHeight;
			}
			else
			{
				BFont.stageWidth = stage.stageWidth;
				BFont.stageHeight = stage.stageHeight;
			}
		}
		
		private function startAll():void
		{
			//开始加载主资源
			var loadingScene:LoadingScene = new LoadingScene("正在加载主资源……");
			SceneManager.getInstance().loadScene(loadingScene);
			ResourceHelper.getInstance().startLoadMainResource(loadingScene.loadBar.updateProgress, main);
			addChild(SceneManager.getInstance());
		}
		
		private function main():void
		{
			PlayerManager.getInstance().addEventListener(ModelEvent.LOGIN_COMPLETE, LoginComplete);
			if (Profile.DEBUG == 1)
			{
				PlayerManager.getInstance().loginMeDebug(4);
			}
			else
			{
				PlayerManager.getInstance().loginMe();
			}

			BSound.getInstance().playMain();
		}
		
		private function LoginComplete(event:Event):void
		{
			PlayerManager.getInstance().removeEventListener(ModelEvent.LOGIN_COMPLETE, LoginComplete);
			SceneManager.getInstance().loadScene(HomeScene.getInstance());
			var me:Player = PlayerManager.getInstance().me;
			//加载建筑
			HomeScene.getInstance().loadPlayer(me);
//			//加载包裹
//			me.packageModel.loadObjects(true, true);
			//加载任务
			me.taskList.loadTasks(true);
			//加载好友
			me.friendList.loadFriends(true);
//			//加载状态
//			me.stateModel.loadState(true);
			//载入公会的信息
			GuildManager.getInstance().loginGuild();
			
			if (me.historyMaxPopulation == 0)
			{
				var prefaceDialog:PrefaceDialog = new PrefaceDialog();
				WidgetManager.getInstance().popOutOneByOne(prefaceDialog);
			}
			else if (me.logonDayCount > 0)
			{
				var dialog:LogonAwardDialog = new LogonAwardDialog(me.logonDayCount, me.logonAwards);
				dialog.x = -dialog.width / 2;
				dialog.y = -dialog.height / 2;
				WidgetManager.getInstance().popOutOneByOne(dialog);
			}
		}
		
	}
}
